package com.software.dao;

import com.software.entity.Invoice;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

public class InvoiceDAO {
    public static void addInvoice(Invoice invoice) {
        // 执行数据库插入操作

        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "insert into invoice_info (invoice_number,issue_date,issuer_name,total_amount) values (?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, invoice.getInvoiceNumber());
            Date currentTime = new Date();
            preparedStatement.setTimestamp(2, new Timestamp(currentTime.getTime()));
            preparedStatement.setString(3, invoice.getIssuerName());
            preparedStatement.setFloat(4, invoice.getTotalAmount());
            //得到结果
            preparedStatement.executeUpdate();
            System.out.println("插入成功");

        } catch (SQLException e) {
            e.printStackTrace();
        }


    }





    public static List<Invoice> getAllInvoices() {

        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "select * from invoice_info order by issue_date desc";
            preparedStatement = connection.prepareStatement(sql);
            //得到结果
            //得到结果
            ResultSet rs = preparedStatement.executeQuery();

            List<Invoice> list = new ArrayList<>();
            while (rs.next()){
                Invoice invoice = new Invoice();
                invoice.setId(rs.getInt("id"));
                invoice.setInvoiceNumber(rs.getString("invoice_number"));
                invoice.setIssueDate(rs.getDate("issue_date"));
                invoice.setIssuerName(rs.getString("issuer_name"));
                invoice.setTotalAmount(rs.getFloat("total_amount"));
                invoice.setInvoiceStatus(rs.getInt("invoice_status"));
                list.add(invoice);
            }

            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }



    public static void cancelInvoice(int invoiceNumber) {


        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "update invoice_info set invoice_status = 0 where id = ?";
            preparedStatement.setString(1, "%"+invoiceNumber+"%");
            preparedStatement = connection.prepareStatement(sql);

            //得到结果
            preparedStatement.executeUpdate();
            System.out.println("更新成功");

        } catch (SQLException e) {
            e.printStackTrace();
        }



    }


    public static List<Invoice> getInvoicesByName(String issuerName) {


        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "select * from invoice_info where issuer_name like ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "%"+issuerName+"%");
            //得到结果
            //得到结果
            ResultSet rs = preparedStatement.executeQuery();

            List<Invoice> list = new ArrayList<>();
            while (rs.next()){
                Invoice invoice = new Invoice();
                invoice.setId(rs.getInt("id"));
                invoice.setInvoiceNumber(rs.getString("invoice_number"));
                invoice.setIssueDate(rs.getDate("issue_date"));
                invoice.setIssuerName(rs.getString("issuer_name"));
                invoice.setTotalAmount(rs.getFloat("total_amount"));
                invoice.setInvoiceStatus(rs.getInt("invoice_status"));
                list.add(invoice);
            }

            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static List<Invoice> getInvoicesByNumber(String invoiceNumber) {

        ConnectionDao connectionDao = new ConnectionDao();
        Connection connection = connectionDao.getConnection();


        //执行sql语句的statement
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        try {
            statement = connection.createStatement();
            //SQL语句
            String sql = "select * from invoice_info where invoice_number = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, invoiceNumber);
            //得到结果
            //得到结果
            ResultSet rs = preparedStatement.executeQuery();

            List<Invoice> list = new ArrayList<>();
            while (rs.next()){
                Invoice invoice = new Invoice();
                invoice.setId(rs.getInt("id"));
                invoice.setInvoiceNumber(rs.getString("invoice_number"));
                invoice.setIssueDate(rs.getDate("issue_date"));
                invoice.setIssuerName(rs.getString("issuer_name"));
                invoice.setTotalAmount(rs.getFloat("total_amount"));
                invoice.setInvoiceStatus(1);
                list.add(invoice);
            }

            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;

    }
}
